package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.DownDataVo;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sqfwDoc.dto.CompleteFlowTemplateCodeUpdateDTO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.enums.weiqingmen.SafetyCheckEnum;
import com.budwk.app.sqfwV2.service.weiqingmen.SafetyCheckExcelService;
import com.budwk.app.sqfwV2.service.weiqingmen.SafetyCheckPdfService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiFormParams;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 *
 * 威清门消防安全排查 SafetyCheck
 */
@IocBean
@At("/platform/sqfw/weiQingMenSafetyChec")
@ApiDefinition(tag = "消防信息")
@Slf4j
public class WeiQingMenSafetyCheckController {

    @Inject
    SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    private SafetyCheckPdfService safetyCheckPdfService;
    @Inject
    private SafetyCheckExcelService safetyCheckExcelService;

    @At("/v2/submitData")
    @Ok("json")
    @POST
    @ApiOperation(name = "提交数据/生成PDF, 2.0版本")
    @ApiFormParams(implementation = TemplateCodeUpdateDTO.class)
    @SLog(tag = "新增接口", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> submitData(@Param("..") TemplateCodeUpdateDTO dto) {
        safetyCheckPdfService.submitData(dto);
        return Result.success();
    }

    @At("/v2/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取列表数据 2.0版本")
    @ApiResponses
    @SaCheckLogin
    public Result<?> list(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,
                          @Param("sort") String sort, @Param("fillDate") String fillDate) {
        return safetyCheckPdfService.list(pageNo, pageSize, fillDate, sort);
    }

    @At("/v2/getFormSummaryByShopId")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取店铺最新一条巡查记录 2.0版本")
    @ApiResponses
    @SaCheckLogin
    public Result<?> getFormSummaryByShopId(@Param("shopId") String shopId) {
        return Result.data(safetyCheckPdfService.getFormSummaryByShopId(shopId));
    }

    @At("/v2/update")
    @Ok("json")
    @POST
    @ApiOperation(name = "更新 2.0版本")
    @SaCheckLogin
    public Result<?> update(@Param("..") TemplateCodeUpdateDTO dto) {
        safetyCheckPdfService.update(dto);
        return Result.success();
    }

    @At("/v2/checkData")
    @Ok("json")
    @POST
    @ApiOperation(name = "数据检测 2.0版本")
    @SaCheckLogin
    public Result<?> checkData(@Param("..") DownExcelLedgerReqVo reqVo) {

        List<DownFileTypesResVo> downFileTypesResVos = new ArrayList<>();

        reqVo.setCheckData(true);

        // excel
        DownDataVo<ResultOfExcelGenerator> downDataVo = safetyCheckExcelService.createExcel(reqVo);
        List<ResultOfExcelGenerator> generateExcelUrlVOList = downDataVo.getVoList();
        for (ResultOfExcelGenerator vo : generateExcelUrlVOList) {
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            SafetyCheckEnum excel = SafetyCheckEnum.getEnum(vo.getFileIdentity());
            resVo.setLabel(excel.getName())
                    .setPreviewImage(List.of(excel.getImg().split(",")))
                    .setFileCode(String.valueOf(excel.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(excel.getCode()))
                    .setFileName(excel.getCompilationName())
                    .setFileType(excel.getFileType())
                    .setHiveData(vo.getPassCreateConditionFlag() == null ? false : vo.getPassCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }

        // pdf
        List<ResultOfExcelGenerator> generatePdfUrlVOList = safetyCheckPdfService.downPdfList(reqVo);
        for (ResultOfExcelGenerator vo : generatePdfUrlVOList) {
            SafetyCheckEnum pdf = SafetyCheckEnum.getEnum(vo.getFileIdentity());
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setLabel(pdf.getName())
                    .setPreviewImage(List.of(pdf.getImg().split(",")))
                    .setFileCode(String.valueOf(pdf.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(pdf.getCode()))
                    .setFileName(pdf.getCompilationName())
                    .setFileType(pdf.getFileType())
                    .setHiveData(vo.getPassCreateConditionFlag() == null ? false : vo.getPassCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }

        return Result.data(NutMap.NEW().addv("list", downFileTypesResVos).addv("totalCount", downDataVo.getTotalCount()));
    }

    @At("/v2/downPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "下载pdf 2.0版本")
    @SaCheckLogin
    public Result<?> downPdf(@Param("..") DownExcelLedgerReqVo reqVo) {
        List<ResultOfExcelGenerator> generatePdfUrlVOList = safetyCheckPdfService.downPdfList(reqVo);
        return Result.data(generatePdfUrlVOList);
    }

    @At("/v2/downExcel")
    @Ok("json")
    @POST
    @ApiOperation(name = "下载excel 2.0版本")
    @SaCheckLogin
    public Result<?> downExcel(@Param("..") DownExcelLedgerReqVo reqVo) {
        DownDataVo<ResultOfExcelGenerator> downDataVo = safetyCheckExcelService.createExcel(reqVo);
        return Result.data(downDataVo.getVoList());
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "保存数据并创建pdf")
    @SLog(tag = "保存数据并创建pdf", value = "${args[0]}")
    public Result<?> createPdfAndMsg(@Param("..") CompleteFlowTemplateCodeUpdateDTO data) {
        log.info("威清门消防安全排查：" + Json.toJson(data));
        safetyCheckPdfService.createPdfAndMsg(data);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "重新创建pdf")
    @SLog(tag = "重新创建pdf", value = "${args[0]}")
    public Result<?> reCreatePdfAndMsg(@Param("completeFlowId") String completeFlowId) {
        safetyCheckPdfService.reCreatePdfAndMsg(completeFlowId);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载excel（数据检测、模板下载、数据下载都用此接口）")
    @SLog(tag = "下载excel", value = "${args[0]}")
    public Result<?> downExcel(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList,
                                 @Param("sqfwIdList") List<String> sqfwIdList) {
        List<ResultOfExcelGenerator> excelList = safetyCheckExcelService.createExcel(excelConfigList, sqfwIdList);
        return Result.success(excelList);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "pdf合集下载（数据检测、模板下载、数据下载都用此接口）")
    @ApiResponses
    @SLog(tag = "pdf合集下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downPdfList(@Param("excelConfigList") List<ExcelCreateConfig> excelConfigList,
                                 @Param("sqfwIdList") List<String> sqfwIdList) {
        return Result.success(safetyCheckPdfService.downPdfList(excelConfigList, sqfwIdList));
    }

    /**
     * 获取回显列表
     * @param name
     * @return
     */
    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取回显列表")
    @ApiResponses
    @SLog(tag = "获取回显列表", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> echo(@Param("name") String name, @Param("address") String address) {
        return Result.success(safetyCheckPdfService.echo(name, address));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取详情")
    @ApiResponses
    @SLog(tag = "获取详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> get(@Param("id") String id) {
        return Result.success(sqfwFormsummaryNewService.fetch(id));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取详情")
    @ApiResponses
    @SLog(tag = "获取详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getToFlowId(@Param("flowId") String flowId) {
        return Result.success(sqfwFormsummaryNewService.getToFlowId(flowId));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新危房图片")
    @ApiResponses
    @SLog(tag = "更新危房图片", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> updateDangerous(@Param("..") Sqfw_formSummary_new formSummary) {
        sqfwFormsummaryNewService.update(formSummary);
        safetyCheckPdfService.reCreatePdfAndMsg(formSummary.getSqfwFlowId());
        return Result.success();
    }
}
